﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace WebServer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Server server = new Server();
        
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            DialogResult result = dialog.ShowDialog();

            rootdirectory.Text = dialog.SelectedPath;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            IPAddress ipAddress = IPAddress.Parse(ip1.Text.ToString() + "." + ip2.Text.ToString() + "." + ip3.Text.ToString() + "." + ip4.Text.ToString());
            
            try
            {
                server.Start(ipAddress, Convert.ToInt32(port.Text), 100, rootdirectory.Text);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void stop_Click(object sender, RoutedEventArgs e)
        {
            server.Stop();
            start.IsEnabled = true;
            test.IsEnabled = false;
            stop.IsEnabled = false;
        }

        private void test_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("explorer.exe", "http://" + ip1.Text.ToString() + "." + ip2.Text.ToString() + "." + ip3.Text.ToString() + "." + ip4.Text.ToString() + ":"+port.ToString());
                System.Windows.MessageBox.Show("Serwer uruchomiono pomyślnie", "Test serwera", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Serwera nie uruchomiono pomyślnie! Przyczyna: " + ex.Message, "Test", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            DataReceive datarecv = new DataReceive();
            datarecv.ShowDialog();
        }
    }
}
